package Example.Components;

import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.objectweb.fractal.api.control.BindingController;
import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.Interface;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.util.Fractal;

import Example.Interfaces.Account;
import Example.Interfaces.Bike;
import Example.Interfaces.Main;


public class Vendor1 implements Main, BindingController, Runnable {

		//define required interfaces
		public String[] listFc() { return new String[]{"Account,Bike"};}
		
	   	public Object lookupFc(String cItf) {
	   		if (cItf.equals("Account")) 
		   		return Account;
		   	if(cItf.equals("Bike"))
		   		return Bike;
	   	return null; 
	   	}
	   	
	   	public void bindFc(String cItf, Object sItf)
	   	{ 	
	   	if (cItf.equals("Account")) 
	   		Account=(Account)sItf;
	   	if(cItf.equals("Bike"))
	   		Bike=(Bike)sItf;
	   	}
	   	
	   	public void unbindFc(String cItf)
	   	{ 
	   		if (cItf.equals("Account")) 
		   		Account=null;
		   	if(cItf.equals("Bike"))
		   		Bike=null;
	   	}
	   	
		
		public void run() {
		
			//basket.rentB(789, 123);
			// TODO Auto-generated method stub
			Display display = Display.getDefault();
			Shell sShell=createSShell();
			sShell.open();

			while (!sShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
			//display.dispose();

		}
		//user information
		private int userID;
		private String userPassword;
	   	private Account Account;  //  @jve:decl-index=0:
	   	private Bike Bike;
		private Object[] userInfor;
		private boolean rent=false;
	   	
		private Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="11,11"
		private Label cardPass_label = null;
		private Text password_text = null;
		private Text cardName_Text = null;
		private Label cardName_Label = null;
		private Button check_button = null;
		private Group BikeInfor_group = null;
		private Group userInfor_group = null;
		private Label userFname_label = null;
		private Label userLname_label = null;
		private Text userFName_text = null;
		private Text userLname_text = null;
		private Label address_label = null;
		private Text address_text = null;
		private Group account_group = null;
		private Label accountRest_label = null;
		private Text accountRest_text = null;
		private Label location_label = null;
		private Text location_text = null;
		private Button findBike_button = null;
		private Table bikes_table = null;
		private Label tripSum_label = null;
		private Text tripSum_text = null;
		private Label distance_label = null;
		private Text distance_text = null;
		private Label locationR_label=null;
		private Text locationR_text=null;
		
		private void createBikeInfor_group() {
			BikeInfor_group = new Group(sShell, SWT.NONE);
			BikeInfor_group.setText("Bike Information");
			BikeInfor_group.setLayout(null);
			BikeInfor_group.setBounds(new Rectangle(323, 111, 241, 151));
			bikes_table = new Table(BikeInfor_group, SWT.BORDER | SWT.V_SCROLL);
			bikes_table.setHeaderVisible(true);
			bikes_table.setLinesVisible(true);
			bikes_table.setBounds(new Rectangle(8, 17, 226, 124));
			
			final TableColumn ct1 = new TableColumn(bikes_table, SWT.NONE);
			ct1.setWidth(60);
			ct1.setText("ID");
		

	        final TableColumn ct2 = new TableColumn(bikes_table, SWT.NONE);
	        ct2.setWidth(60);
	        ct2.setText("Trip");
	        
	        final TableColumn ct3 = new TableColumn(bikes_table, SWT.NONE);
	        ct3.setWidth(102);
	        ct3.setText("Location");
	        
		}

		/**
		 * This method initializes userInfor_group	
		 *
		 */
		private void createUserInfor_group() {
			userInfor_group = new Group(sShell, SWT.NONE);
			userInfor_group.setText("User Information");
			userInfor_group.setLayout(null);
			userInfor_group.setBounds(new Rectangle(14, 83, 252, 112));
			userFname_label = new Label(userInfor_group, SWT.NONE);
			userFname_label.setText("First Name");
			userFname_label.setBounds(new Rectangle(8, 21, 51, 13));
			userFName_text = new Text(userInfor_group, SWT.BORDER);
			userFName_text.setBounds(new Rectangle(64, 18, 174, 19));

			userLname_label = new Label(userInfor_group, SWT.NONE);
			userLname_label.setText("Last Name");
			userLname_label.setBounds(new Rectangle(8, 45, 50, 13));
			userLname_text = new Text(userInfor_group, SWT.BORDER);
			userLname_text.setBounds(new Rectangle(64, 42, 174, 19));
			address_label = new Label(userInfor_group, SWT.NONE);
			address_label.setText("Address");
			address_label.setBounds(new Rectangle(8, 69, 39, 13));
			address_text = new Text(userInfor_group, SWT.BORDER);
			address_text.setBounds(new Rectangle(64, 66, 174, 38));
		}


		/**
		 * This method initializes account_group	
		 *
		 */
		private void createAccount_group() {
			account_group = new Group(sShell, SWT.NONE);
			account_group.setText("Account Information");
			account_group.setLayout(null);
			account_group.setBounds(new Rectangle(14, 208, 254, 143));
			accountRest_label = new Label(account_group, SWT.NONE);
			accountRest_label.setText("Account rest:");
			accountRest_label.setBounds(new Rectangle(7, 20, 68, 17));
			accountRest_text = new Text(account_group, SWT.BORDER);
			accountRest_text.setBounds(new Rectangle(76, 21, 166, 20));
			rentState_label = new Label(account_group, SWT.NONE);
			rentState_label.setBounds(new Rectangle(7, 45, 68, 17));
			rentState_label.setText("Rent_state:");
			rentstate_text = new Text(account_group, SWT.BORDER);
			rentstate_text.setBounds(new Rectangle(76, 45, 166, 20));
			tripSum_label = new Label(account_group, SWT.NONE);
			tripSum_label.setBounds(new Rectangle(7, 70, 68, 17));
			tripSum_label.setText("TripSum:");
			tripSum_text = new Text(account_group, SWT.BORDER);
			tripSum_text.setBounds(new Rectangle(76, 69, 166, 20));
			rentBID_label = new Label(account_group, SWT.NONE);
			rentBID_label.setBounds(new Rectangle(7, 95, 68, 17));
			rentBID_label.setText("RentBike:");
			rentBID_text = new Text(account_group, SWT.BORDER);
			rentBID_text.setBounds(new Rectangle(76, 93, 166, 20));
			rentTime_label= new Label(account_group, SWT.NONE);
			rentTime_label.setBounds(new Rectangle(7, 120, 68, 17));
			rentTime_label.setText("Rent time:");
			rentTime_text=new Text(account_group, SWT.BORDER);
			rentTime_text.setBounds(new Rectangle(76, 117, 166, 20));
		}
		private Label rentTime_label;
		private Text rentTime_text;
		private Label rentBID_label=null;
		private Text rentBID_text=null;
		private Label locationEnd_label = null;
		private Text locationEnd_text = null;
		private Label rentState_label = null;
		private Text rentstate_text = null;
		private Group return_group = null;
		private Label bikeIDreturn_label = null;
		private Text bikeIDreturn_text = null;
		private Label trip_label = null;
		private Text trip_text = null;
		private Button return_button = null;
		

		private void createReturn_group() {
			return_group = new Group(sShell, SWT.NONE);
			return_group.setText("Return");
			return_group.setLayout(null);
			return_group.setBounds(new Rectangle(323, 311, 251, 92));
			bikeIDreturn_label = new Label(return_group, SWT.NONE);
			bikeIDreturn_label.setText("BikeID:");
			bikeIDreturn_label.setBounds(new Rectangle(7, 21, 35, 15));
			bikeIDreturn_text = new Text(return_group, SWT.BORDER);
			bikeIDreturn_text.setBounds(new Rectangle(53, 18, 126, 19));
			Label filler = new Label(return_group, SWT.NONE);
			filler.setBounds(new Rectangle(123, 21, 0, 13));
			trip_label = new Label(return_group, SWT.NONE);
			trip_label.setText("Trip:");
			trip_label.setBounds(new Rectangle(7, 47, 35, 15));
			trip_text = new Text(return_group, SWT.BORDER);
			trip_text.setBounds(new Rectangle(53, 44, 127, 19));
			locationR_label = new Label(return_group, SWT.NONE);
			locationR_label.setText("Loc:");
			locationR_label.setBounds(new Rectangle(7, 73, 35, 15));
			locationR_text = new Text(return_group, SWT.BORDER);
			locationR_text.setBounds(new Rectangle(53, 70, 127, 19));
			
			return_button = new Button(return_group, SWT.NONE);
			return_button.setText("Return");
			return_button.setBounds(new Rectangle(190, 42, 45, 23));
			return_button.setEnabled(false);
			return_button
			.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
				public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
					int bikeID=Integer.parseInt(bikeIDreturn_text.getText());
					String location=locationR_text.getText();
					System.out.println(System.currentTimeMillis());
					double trip=Double.parseDouble(trip_text.getText());
					Bike.returnB(userID,bikeID,location, trip);
				}
				public void widgetDefaultSelected(
						org.eclipse.swt.events.SelectionEvent e) {
				}
			});
		}


		/**
		 * This method initializes sShell
		 */
		private Shell createSShell() {
			sShell = new Shell();
			sShell.setSize(new Point(633, 476));
			sShell.setText("Rent Bike System");
			sShell.setSize(new Point(630, 438));
			sShell.setLayout(null);
			cardName_Text = new Text(sShell, SWT.BORDER);
			cardName_Text.setBounds(new Rectangle(76, 15, 169, 18));
			
			cardName_Label = new Label(sShell, SWT.NONE);
			cardName_Label.setBounds(new Rectangle(15, 15, 61, 18));
			cardName_Label.setText("Card ID:");
			password_text = new Text(sShell, SWT.BORDER);
			password_text.setBounds(new Rectangle(76, 39, 169, 16));
			check_button = new Button(sShell, SWT.NONE);
			check_button.setBounds(new Rectangle(16, 59, 58, 20));
			check_button.setText("CheckID");
			check_button
					.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
						public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
							System.out.println("login"); // TODO Auto-generated Event stub widgetSelected()
							String a=cardName_Text.getText();
							userPassword=password_text.getText();
							userID=Integer.parseInt(a);
							System.out.println("h111");
							userInfor=Account.login(userID, userPassword);
							System.out.println("h2222");
							userFName_text.setText(userInfor[2].toString());
							userLname_text.setText(userInfor[3].toString());
							address_text.setText(userInfor[4].toString());
							accountRest_text.setText(userInfor[5].toString());
							tripSum_text.setText(userInfor[6].toString());
							rentTime_text.setText(userInfor[9].toString());
							if(userInfor[7].toString().equals("true")){
								rentstate_text.setText("under rent");
								rent_button.setEnabled(false);
								return_button.setEnabled(true);
							}
							else{
								rentstate_text.setText("no rent");
								rent_button.setEnabled(true);
								return_button.setEnabled(false);
							}
							if(!userInfor[8].toString().equals("0"))
								rentBID_text.setText(userInfor[8].toString());
							else
								rentBID_text.setText("no rent");
							
						}
						public void widgetDefaultSelected(
								org.eclipse.swt.events.SelectionEvent e) {
								;
									
						}
					});
			createBikeInfor_group();
			createUserInfor_group();
			createAccount_group();
			
			location_label = new Label(sShell, SWT.NONE);
			location_label.setBounds(new Rectangle(308, 15, 74, 18));
			location_label.setText("Location start: ");
			location_text = new Text(sShell, SWT.BORDER);
			location_text.setBounds(new Rectangle(382, 15, 126, 18));
			locationEnd_label = new Label(sShell, SWT.NONE);
			locationEnd_label.setBounds(new Rectangle(308, 45, 74, 18));
			locationEnd_label.setText("Location end:");
			locationEnd_text = new Text(sShell, SWT.BORDER);
			locationEnd_text.setBounds(new Rectangle(382, 45, 126, 18));
			distance_label = new Label(sShell, SWT.NONE);
			distance_label.setBounds(new Rectangle(308, 75, 74, 18));
			distance_label.setText("Distance:");
			distance_text = new Text(sShell, SWT.BORDER);
			distance_text.setBounds(new Rectangle(382, 75, 126, 18));
			
			findBike_button = new Button(sShell, SWT.NONE);
			findBike_button.setBounds(new Rectangle(516, 15, 81,20 ));
			findBike_button.setText("Find bikes");
			findBike_button
			.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
				public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
					System.out.println("findBike"); // TODO Auto-generated Event stub widgetSelected()
					Vector bikes=new Vector();
					bikes=Bike.findBike(location_text.getText(), locationEnd_text.getText());
					int i=bikes.size();
					distance_text.setText((String) bikes.get(i-1));
					for (int j = 0; j < bikes.size()-1; j++) {
						Object[] b=(Object[]) bikes.get(j);
						TableItem item = new TableItem(bikes_table, SWT.NONE);
						item.setText( new String[] { b[0].toString(),b[1].toString(),b[2].toString() } );
					}
				}
				public void widgetDefaultSelected(
						org.eclipse.swt.events.SelectionEvent e) {
				}
			});
			
			cardPass_label = new Label(sShell, SWT.NONE);
			cardPass_label.setBounds(new Rectangle(15, 38, 61, 18));
			cardPass_label.setText("Password:");
			createReturn_group();
			createRent_group();
			createPay_group();
			return sShell;
		}

		private Group rent_group = null;
		private Label bikeIDrent_label = null;
		private Text bikeIDrent_text = null;
		private Button rent_button = null;

		/**
		 * This method initializes rent_group	
		 *
		 */
		private void createRent_group() {
			rent_group = new Group(sShell, SWT.NONE);
			rent_group.setText("Rent");
			rent_group.setLayout(null);
			rent_group.setBounds(new Rectangle(323, 262, 252, 43));
			bikeIDrent_label = new Label(rent_group, SWT.NONE);
			bikeIDrent_label.setText("BikeID:");
			bikeIDrent_label.setBounds(new Rectangle(7, 17, 35, 15));
			rent_button = new Button(rent_group, SWT.NONE);
			rent_button.setText("Rent");
			rent_button.setBounds(new Rectangle(192, 17, 35, 20));
			rent_button.setEnabled(false);
			rent_button
			.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
				public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
					int i;
					i=Integer.valueOf(bikeIDrent_text.getText()).intValue();
					Bike.rentB(i, userID);
				}
				public void widgetDefaultSelected(
						org.eclipse.swt.events.SelectionEvent e) {
				}
			});
			bikeIDrent_text = new Text(rent_group, SWT.BORDER);
			bikeIDrent_text.setBounds(new Rectangle(52, 16, 127, 19));
		}
		private Group pay_group = null;
		private Button pay_button = null;
		private Text cost_text = null;
		private Label cost_label = null;
		/**
		 * This method initializes pay_group	
		 *
		 */
		private void createPay_group() {
			pay_group = new Group(sShell, SWT.NONE);
			pay_group.setLayout(null);
			pay_group.setBounds(new Rectangle(15, 350, 256, 43));
			pay_button = new Button(pay_group, SWT.NONE);
			pay_button.setBounds(new Rectangle(199, 12, 39, 23));
			pay_button.setText("Pay");
			cost_text = new Text(pay_group, SWT.BORDER);
			cost_text.setBounds(new Rectangle(65, 16, 125, 18));
			cost_label = new Label(pay_group, SWT.NONE);
			cost_label.setBounds(new Rectangle(9, 15, 52, 19));
			cost_label.setText("Cost:");
		}

	}
